<?php
namespace app\admin\controller;

use app\common\controller\Admin;

class Tag extends Admin
{
    //定义表单字段与属性
    protected function fields($action='create', $data=[])
    {
        //合并初始数据
        $data = DcArrayArgs(config('form_data.admin_tag_'.$action), $data);
        //获取初始化字段与扩展字段
        $fields = \daicuo\Tag::fields($data);
        //所有分类字段预埋钩子
        return DcArrayArgs(config('form_fields.admin_tag_'.$action), $fields);
    }
    
    //添加(表单)
	public function create()
    {
        $data = [
            'term_status' => 'normal',
            'term_module' => DcEmpty($this->query['term_module'], 'common'),
            'term_tpl'    => 'index',
        ];
        
        $this->assign('query', $this->query);
        
        $this->assign('data', $data);
        
        $this->assign('fields', $this->formFields('create', $data));

		return $this->fetch();
	}
    
	//添加(数据库)
	public function save()
    {
        config('common.validate_name', config('validate_name.admin_tag_save'));
        
        config('common.validate_scene', config('validate_scene.admin_tag_save'));
        
        config('common.where_slug_unique', config('slug_unique.admin_tag_save'));
        
        $term_id = \daicuo\Term::save(input('post.'));
        
		if($term_id < 1){
			$this->error(\daicuo\Term::getError());
		}
        
		$this->success(lang('success'));
	}
    
    //删除(数据库)
	public function delete()
    {
		$ids = input('id/a');
		if(!$ids){
			$this->error(lang('mustIn'));
		}
        
        \daicuo\Term::delete_ids($ids);

        $this->success(lang('success'));
	}
    
    //修改（表单）
	public function edit()
    {
		$term_id = input('id/d',0);
		if(!$term_id){
			$this->error(lang('mustIn'));
		}
        
		//查询数据
        $data = \daicuo\Term::get_id($term_id, false);
        if( is_null($data) ){
            $this->error(lang('empty'));
        }
        
        $this->assign('query', $this->query);
        
        $this->assign('data', $data);
        
        $this->assign('fields', $this->formFields('edit', $data));
        
		return $this->fetch();
	}
    
    //修改（数据库）
	public function update()
    {
		$data = input('post.');
        if(!$data['term_id']){
            $this->error(lang('mustIn'));
        }
        
        config('common.validate_name', config('validate_name.admin_tag_update'));
        
        config('common.validate_scene', config('validate_scene.admin_tag_update'));
        
        config('common.where_slug_unique', config('slug_unique.admin_tag_update'));
        
        $info = \daicuo\Term::update_id($data['term_id'], $data);
        if(is_null($info)){
            $this->error(\daicuo\Term::getError());
        }
        $this->success(lang('success'));
	}
    
	public function index()
    {
        if($this->request->isAjax()){
            //查询参数
            $args = array();
            $args['cache']         = false;
            $args['field']         = '*';
            $args['type']          = 'tag';
            $args['limit']         = input('pageSize/d', 30);
            $args['page']          = input('pageNumber/d', 1);
            $args['sort']          = input('get.sortName/s','term_id');
            $args['order']         = input('get.sortOrder/s','desc');
            $args['search']        = input('searchText/s','');
            //扩展字段生成查询参数
            $args['meta_query']    = [];
            foreach( DcArrayFilter($this->query, config('custom_fields.term_meta')) as $key=>$value ){
                if($value){
                    array_push($args['meta_query'], ['key'=>['eq',$key],'value'=>['eq',$value]]);
                }
            }
            //自定义查询条件
            if( $where = DcWhereQuery(['term_order','term_module','term_status'], 'eq', $this->query) ){
                $args['where'] = $where;
            }
            //数据返回
            if($infos = DcTermSelect($args) ){
                return json($infos);
            }
            return json([]);
		}
       //地址栏参数
        $this->assign('query', $this->query);
        //表单筛选字段
        $this->assign('fields', $this->formFields('index', $this->query));
        //表格列字段
        $this->assign('columns', $this->tableColumns('index', $this->query));
        //加载模板
		return $this->fetch();
	}
}